package com.hx.myfristandriod;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.hx.dao.DiaryDao;
import com.hx.sqliteHelper.SQLliteHelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TestSqliteActivity extends Activity {

	private DiaryDao diaryDao;
	private Button btnAdd;
	private Button btnDelete;
	private Button btnUpdate;
	private Button btnQuery;
	    @Override
		protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.sqlite_test);
	        //连接数据库
	         initDataBase();
	        diaryDao=new DiaryDao(TestSqliteActivity.this);
	        //获取按钮对象
	        btnAdd=(Button) findViewById(R.id.btnAdd);
	        btnDelete=(Button) findViewById(R.id.btnDelete);
	        btnUpdate=(Button) findViewById(R.id.btnUpdate);
	        btnQuery=(Button) findViewById(R.id.btnQuery);
	        //绑定监听
	        btnAdd.setOnClickListener(new ViewOcl());
	        btnDelete.setOnClickListener(new ViewOcl());
	        btnUpdate.setOnClickListener(new ViewOcl());
	        btnQuery.setOnClickListener(new ViewOcl());
	    }

	    //btn绑定监听事件，发送信息单击调用
  class ViewOcl implements OnClickListener{
	@Override
	public void onClick(View v) {
		String sql="";
		boolean flag=false;
		String message="";
      switch (v.getId()) {
	       case R.id.btnAdd:
		        String title="测试标题1";
		        String weather="晴";
		        String context="测试内容1。。。";
		        String publish=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		        sql="insert into diary values(null,'"+title+"','"+weather+"','"+context+"','"+publish+"')";
		       flag= diaryDao.execOther(sql);
		       message=  flag?"添加成功":"添加失败";
		       Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
			 break;
	       case R.id.btnQuery:
		   		 sql="select * from diary";
		     	String data= diaryDao.execQuery(sql);
		     	 Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
				 break;
	       case R.id.btnDelete:
		        sql="delete from diary where tid=1";
		       flag= diaryDao.execOther(sql);
		       message=  flag?"删除成功":"删除失败";
		       Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
				 break;
	       case R.id.btnUpdate:
		        sql="update  diary set title=12123 where tid=1";
		       flag= diaryDao.execOther(sql);
		       message=  flag?"更新成功":"更新失败";
		       Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
				 break;
	     
	  	default:
			break;
         	}
	} 
  }
  
   private void initDataBase(){
		 SQLliteHelper helper=new SQLliteHelper(TestSqliteActivity.this);
		 helper.getWritableDatabase();
		  System.out.println("数据库连接成功。。。");
	  }
  
}
